﻿using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using SurveySez.Model;
using SurveySez.NavigationModel;
using UpdateControls.XAML;
using System;

namespace SurveySez.ViewModel.Editor
{
    public class EditorViewModel
	{
		private Emcee _emcee;
		private EditorNavigationModel _navigation;
		private IGamePresenter _gamePresenter;

		public EditorViewModel(Emcee emcee, EditorNavigationModel navigation, IGamePresenter gamePresenter)
		{
			_emcee = emcee;
			_navigation = navigation;
			_gamePresenter = gamePresenter;
		}

		public IEnumerable<EmceeSurveyHeaderViewModel> Surveys
		{
			get
			{
				return _emcee.EmceeSurveys
					.OrderBy(emceeSurvey => emceeSurvey.Survey.Question)
					.Select(emceeSurvey => new EmceeSurveyHeaderViewModel(emceeSurvey));
			}
		}

		public EmceeSurveyHeaderViewModel SelectedSurvey
		{
			get
			{
				return _navigation.SelectedSurvey == null
					? null
					: new EmceeSurveyHeaderViewModel(_navigation.SelectedSurvey);
			}
			set
            {
            	_navigation.SelectedSurvey = value == null
					? null
					: value.EmceeSurvey;
            }
		}

		public ICommand NewSurvey
		{
			get
			{
				return MakeCommand
					.Do(() => _navigation.SelectedSurvey = _emcee.NewSurvey());
			}
		}

		public ICommand DeleteSurvey
		{
			get
			{
				return MakeCommand
					.When(() => _navigation.SelectedSurvey != null)
					.Do(() => _navigation.SelectedSurvey.Delete());
			}
		}

		public ICommand ViewSurvey
		{
			get
			{
				return MakeCommand
					.When(() => _navigation.SelectedGameSurvey != null)
					.Do(() => _navigation.SelectedSurvey = _emcee.EmceeSurveys.FirstOrDefault(emceeSurvey => emceeSurvey.Survey == _navigation.SelectedGameSurvey.Survey));
			}
		}

		public SurveyDetailsViewModel SurveyDetails
		{
			get
			{
				return _navigation.SelectedSurvey == null
					? null
					: new SurveyDetailsViewModel(_navigation.SelectedSurvey.Survey, _navigation);
			}
		}

		public IEnumerable<GameHeaderViewModel> Games
		{
			get
			{
				return _emcee.EmceeGames
					.OrderBy(emceeGame => emceeGame.Game.Name)
					.Select(emceeGame => new GameHeaderViewModel(emceeGame));
			}
		}

		public GameHeaderViewModel SelectedGame
		{
			get
			{
				return _navigation.SelectedGame == null
					? null
					: new GameHeaderViewModel(_navigation.SelectedGame);
			}
			set
            {
            	_navigation.SelectedGame = value == null
					? null
					: value.EmceeGame;
            }
		}

		public ICommand NewGame
		{
			get
			{
				return MakeCommand
					.Do(() => _navigation.SelectedGame = _emcee.NewGame());
			}
		}

		public ICommand DeleteGame
		{
			get
			{
				return MakeCommand
					.When(() => _navigation.SelectedGame != null)
					.Do(() => _navigation.SelectedGame.Delete());
			}
		}

		public ICommand PlayGame
		{
			get
			{
				return MakeCommand
					.When(() => _navigation.SelectedGame != null && _gamePresenter.GameInPlay == null)
					.Do(() => _gamePresenter.GameInPlay = _navigation.SelectedGame.Game);
			}
		}

		public GameDetailsViewModel GameDetails
		{
			get
			{
				return _navigation.SelectedGame == null
					? null
					: new GameDetailsViewModel(_navigation.SelectedGame, _navigation);
			}
		}
	}
}
